﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace SvnCommitNotifier {
    internal static class SvnWorkCopyMonitor {

        /// <summary>
        /// 监视对象字典
        /// </summary>
        private static IDictionary<string, FileSystemWatcher> watchers = new Dictionary<string, FileSystemWatcher>();

        /// <summary>
        /// SVN程序主目录
        /// </summary>
        private static readonly string SVN_HOME = @"C:\Program Files\VisualSVN Server\bin";

        /// <summary>
        /// 文件更改消息
        /// </summary>
        private static IDictionary<string, string> messages = new Dictionary<string, string>();

        /// <summary>
        /// 添加监视路径
        /// </summary>
        /// <param name="path">路径</param>
        public static void AddPath(string path) {
            if (!watchers.ContainsKey(path)) {
                var watcher = new FileSystemWatcher(path);
                watcher.Changed += OnFileChanged;
                watcher.WaitForChanged(WatcherChangeTypes.All);
                watchers.Add(path, watcher);
                Trace.WriteLine(path + " added");
            }
        }

        /// <summary>
        /// 移除监视路径
        /// </summary>
        /// <param name="path">路径</param>
        public static void RemovePath(string path)
        {
            
        }

        /// <summary>
        /// 文件修改事件
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <param name="e">事件参数</param>
        private static void OnFileChanged(object sender, FileSystemEventArgs e)
        {
            Trace.WriteLine(e.FullPath);
        }

        /// <summary>
        /// 获取对应SVN路径的状态
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>SVN状态</returns>
        public static IList<SvnFileStatus> GetSvnStatus(string path)
        {
            StreamWriter sw;
            StreamReader sr;
            StreamReader err;
            Process p = new Process();
            ProcessStartInfo psi = new ProcessStartInfo(Environment.GetEnvironmentVariable("ComSpec"));
            psi.UseShellExecute = false;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.CreateNoWindow = true;
            psi.WorkingDirectory = SVN_HOME;
            p.StartInfo = psi;
            p.Start();

            sw = p.StandardInput;
            sr = p.StandardOutput;
            err = p.StandardError;

            sw.AutoFlush = true;
            sw.WriteLine("svn.exe status \"" + path + "\">&2");
            sw.Close();

            var status = err.ReadToEnd();
            var statuses = status.Replace("\r", String.Empty).Split('\n');
            var list = new List<SvnFileStatus>();
            foreach (string line in statuses)
            {
                var str = line.Trim();
                if (!String.IsNullOrEmpty(str) && str.Length > 8)
                {
                    list.Add(new SvnFileStatus(line, line.Substring(7)));
                }
            }

            return list;
        }
    }
}
